package com.parking.common.web.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisObjectTemplate;
    /**
     * 设置不含时间的。
     */
    public void set(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }
    /**
     * 新增字符串数据,有过期时间
     */
    public void set(String key,String value,Long expire){
        stringRedisTemplate.opsForValue().set(key,value,expire,TimeUnit.SECONDS);
    }
    /**
     * 查看
     */
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void del(String... key){
        stringRedisTemplate.delete(Arrays.asList(key));
    }
    /**
     * 自增1
     */
    public void incr(String key){
        stringRedisTemplate.opsForValue().increment(key);
    }
    /**
     * 判断是否存在
     */
    public boolean exist(String key){
        return stringRedisTemplate.hasKey(key);
    }

    public void setSet(String key, String... values){
        stringRedisTemplate.opsForSet().add(key,values);
    }

    public boolean isSetMember(String key, String value){
        return stringRedisTemplate.opsForSet().isMember(key,value);
    }

    public boolean isZSetMember(String key, String value){
        Double score = stringRedisTemplate.opsForZSet().score(key, value);
        if(score==null) return false;
        return true;
    }

    public Long ZSetSize(String key){
        return stringRedisTemplate.opsForZSet().zCard(key);
    }

    public void delSet(String key,Object... values){
        stringRedisTemplate.opsForSet().remove(key,values);
    }

    public Set<String> getSet(String key){
        return stringRedisTemplate.opsForSet().members(key);
    }

    public void setZSet(String key,String value,double incr){
        stringRedisTemplate.opsForZSet().incrementScore(key,value,incr);
    }

    public Set<String> ZSetRevRank(String key,Integer start,Integer end){
        return stringRedisTemplate.opsForZSet().reverseRange(key,start,end);
    }

    public Set<String> ZSetRank(String key,Integer start,Integer end){
        return stringRedisTemplate.opsForZSet().range(key,start,end);
    }

    public double ZSetScore(String key,String value){
        return stringRedisTemplate.opsForZSet().score(key,value);
    }

    public void ZSetRemove(String key,Object... values){
        stringRedisTemplate.opsForZSet().remove(key,values);
    }

    public Object getObject(String key){
        return redisObjectTemplate.opsForValue().get(key);
    }

    public void setObject(String key, Object value, Long expire){
        redisObjectTemplate.opsForValue().set(key,value,expire);
    }

    public void addHash(String key, HashMap<String,String> map){
        stringRedisTemplate.opsForHash().putAll(key,map);
    }

    public void updateHashByIncr(String key,String property,double number){
        stringRedisTemplate.opsForHash().increment(key,property,number);
    }

    public void updateHash(String key,String property, String number){
        stringRedisTemplate.opsForHash().put(key,property,number);
    }

    public Object getHash(String key,Object value){
        return stringRedisTemplate.opsForHash().get(key,value);
    }

    public HashMap<Object,Object> getHashAll(String key){
        Set<Object> keys = stringRedisTemplate.opsForHash().keys(key);
        HashMap<Object,Object> map = new HashMap<>();
        for (Object hashkey : keys) {
            Object value = stringRedisTemplate.opsForHash().get(key,hashkey);
            map.put(hashkey,value);
        }
        return map;
    }

    public void zadd(String key,String value,double score){
        stringRedisTemplate.opsForZSet().add(key,value,score);
    }
    /**
     * 添加一个Set集合
     * */
    public void sadd(String key, Set<String> set){
        stringRedisTemplate.opsForSet().add(key, set.toArray(new String[0]));
    }
    /**
     * 取出一个Set集合
     *
     * @return
     */
    public Set<String> members(String key){
        return stringRedisTemplate.opsForSet().members(key);
    }
}
